package com.zmis.system.upload.action;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zmis.system.config.SystemSetting;

/**
 * 
 * 2012-9-15 下午01:16:33
 * @author zlj
 */
@Controller("system/upload/fileupload")
@Scope("prototype")
public class FileUpLoad extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(FileUpLoad.class);
	/**前端文件名称name='zfile'*/
	private List<File> zfile;
	/**上传文件名称*/
	private List<String> zfileFileName;
	/**文件类型*/
	private List<String> zfileContentType;
	/**允许上传的文件类型*/
	private String allowedTypes;
	/**文件上传状态*/
	private String message;
	/**文件大小*/
	private List<Long> fileSize;
	/**上传文件路径*/
	private String destDir = ServletActionContext.getServletContext().getRealPath((String) SystemSetting.getValueByKey("destFileDir"));
	
	private static HttpServletRequest request = ServletActionContext.getRequest();

	@Override
	public String execute() throws Exception {
		if(zfile!=null && zfile.size()>0) {
			//开始上传
			for(int i=0; i<zfile.size(); i++) {
				File descFile = new File(destDir, getDesFileName(zfileFileName.get(i)));
				try {
					FileUtils.copyFile(zfile.get(i), descFile);
				} catch (Exception e) {
					logger.error("文件上传错误");
					this.setMessage("文件上传错误");
					return "input";
				}
			}
			this.setMessage("文件上传成功");
			return "success";
		}else {
			this.setMessage("请选择你要上传的文件");
			return "input";
		}
	}
	
	
	/**
	 * 测试阶段 需要测试到添加错误信息到 addActionError中的情况
	 * 这里还需要处理类型问题
	 * 
	 */
	@Override
	public void addFieldError(String fieldName, String errorMessage) {
		//改从国际化里取值   
		System.out.println("addFieldError======================="+errorMessage);
		if (errorMessage.startsWith("the request was rejected because its size") || errorMessage.startsWith("File too large")) {
			this.setMessage("上传文件不能超过30M");
			super.addFieldError(fieldName, errorMessage);
		}else {
			super.addFieldError(fieldName, errorMessage);
		}
	}
	
	@Override
	public void addActionError(String anErrorMessage) {
		System.out.println("addActionError======================="+anErrorMessage);
		if (anErrorMessage.startsWith("the request was rejected because its size") || anErrorMessage.startsWith("File too large")) {
			this.setMessage("上传文件不能超过30M");
			super.addActionError(anErrorMessage);
		}else {
			super.addActionError(anErrorMessage);
		}
	}
	
	public static String getDesFileName(String fileName) {
		String fix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
		String name = fileName.substring(0, fileName.lastIndexOf("."));
		String remoteIP=request.getRemoteAddr();
		long mi = System.currentTimeMillis();
		return name+"("+remoteIP+"-"+mi+")"+fix;
	}

	
	
	//getting/setting
	
	
	public String getAllowedTypes() {
		return allowedTypes;
	}
	public String getMessage() {
		return message;
	}


	public void setMessage(String message) {
		if(this.message==null || this.message.length()==0) {
			this.message = message;
		}
	}


	public void setAllowedTypes(String allowedTypes) {
		this.allowedTypes = allowedTypes;
	}
	
	

	public List<Long> getFileSize() {
		return fileSize;
	}

	public void setFileSize(List<Long> fileSize) {
		this.fileSize = fileSize;
	}

	public List<File> getZfile() {
		return zfile;
	}
	public void setZfile(List<File> zfile) {
		this.zfile = zfile;
	}
	public List<String> getZfileFileName() {
		return zfileFileName;
	}
	public void setZfileFileName(List<String> zfileFileName) {
		this.zfileFileName = zfileFileName;
	}
	public List<String> getZfileContentType() {
		return zfileContentType;
	}
	public void setZfileContentType(List<String> zfileContentType) {
		this.zfileContentType = zfileContentType;
	}
}
